/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tablero.java
 *
 * Created on 19-nov-2011, 13:30:08
 */
package scrum;




 

import Procs.ProcsTablero;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import scrum.proyectos.agregar_tarea;

/**
 *
 * @author jorgeraulgrion
 */
public class Tablero extends javax.swing.JFrame {
     JButton mas;
     int contbuton=0;
     int idTarea=0;
     int idSprint;
     String estadoSprint;
     java.sql.ResultSet rs,rs2;
     ProcsTablero procs;
     TextColorPanel[] colorpan = new TextColorPanel[3];
     private LinkedList<JButton> mBotones = new LinkedList<JButton>();
     private LinkedList<JPopupMenu> mmenus = new LinkedList<JPopupMenu>();
     int corX=2,corY=10,corX2=2,corY2=10,corX3=2,corY3=10;
     int IdTarea=0,contSprint=0;
    /** Creates new form Tablero */
     
     public Tablero(){
         
     }
     
    public Tablero(int idSprint) {
        this.idSprint = idSprint;
        initComponents();
        jComboBox1.setVisible(false);
        
        procs = new ProcsTablero();
        try {
            rs = procs.verEstadoSprint(this.idSprint);
            while (rs.next()){
            estadoSprint=rs.getObject("estado").toString();
            }
            /*ColorJjPanel jpVista = new ColorJjPanel();
            jpVista.setSize(this.getSize());
            jpVista.setVistaColor();
            jPanel1.add(jpVista);*/
        } catch (SQLException ex) {
            Logger.getLogger(Tablero.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        int estadoTarea;
        try {
            rs2 = procs.verdatosTarea();
            while (rs2.next()){
            idTarea = Integer.parseInt(rs2.getObject("idTarea").toString());
            estadoTarea=Integer.parseInt(rs2.getObject("estado").toString());
            if (estadoTarea ==1){
                CrearBoton(jPanel1,idTarea);
            }
             if (estadoTarea ==2){
                CrearBoton(jPanel2,idTarea);
            }
              if (estadoTarea ==3){
                CrearBoton(jPanel3,idTarea);
            }
            
            }
            /*ColorJjPanel jpVista = new ColorJjPanel();
            jpVista.setSize(this.getSize());
            jpVista.setVistaColor();
            jPanel1.add(jpVista);*/
        } catch (SQLException ex) {
            Logger.getLogger(Tablero.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        JOptionPane.showMessageDialog(null, estadoSprint);
        if (estadoSprint.equals("iniciado")){
            BAgregarTarea.setVisible(false);
            jButton1.setVisible(false);
        }
        /*ColorJjPanel jpVista = new ColorJjPanel();
        jpVista.setSize(this.getSize());
        jpVista.setVistaColor();
        jPanel1.add(jpVista);*/ 
        
        ColorPanel(jPanel1,"Inicio",0);
        ColorPanel(jPanel2,"Proceso",1);
        ColorPanel(jPanel3,"Finalizado",2);
        
          
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        BAgregarTarea = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 288, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 386, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(290, 318));

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 288, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 386, Short.MAX_VALUE)
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setPreferredSize(new java.awt.Dimension(290, 318));

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 288, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 386, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(scrum.ScrumApp.class).getContext().getResourceMap(Tablero.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        BAgregarTarea.setFont(resourceMap.getFont("BAgregarTarea.font")); // NOI18N
        BAgregarTarea.setText(resourceMap.getString("BAgregarTarea.text")); // NOI18N
        BAgregarTarea.setName("BAgregarTarea"); // NOI18N
        BAgregarTarea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAgregarTareaActionPerformed(evt);
            }
        });

        jComboBox1.setName("jComboBox1"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 195, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(BAgregarTarea, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 118, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(43, 43, 43)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(29, 29, 29)
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(37, 37, 37)
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(243, 243, 243)
                        .add(jLabel2)
                        .add(234, 234, 234)
                        .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(BAgregarTarea, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 16, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void BAgregarTareaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAgregarTareaActionPerformed
// TODO add your handling code here:
    agregar_tarea tarea = new agregar_tarea(1,1,idTarea);
    tarea.setVisible(true);
    idTarea++;
    CrearBoton(jPanel1,idTarea);
}//GEN-LAST:event_BAgregarTareaActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    procs.ActualizaEstadoSprint(idSprint);
    jButton1.setVisible(false);
    BAgregarTarea.setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
     
     private void PosicionBotonPanel1(){
     corX+=70;    
     if (corX>=282){
         corY+=40;
         corX=2;
     }
     
     }
     
     private void PosicionBotonPanel2(){
     corX2+=70;    
     if (corX2>=282){
         corY2+=40;
         corX2=2;
     }
     }
     
     private void PosicionBotonPanel3(){
     corX3+=70;    
     if (corX3>=282){
         corY3+=40;
         corX3=2;
     }
     }
     
     
     private void CrearBoton(JPanel panel, int idtarea){
        contbuton++;
        QuitarColorPanel(); 
        mas = new JButton();
        panel.add(mas);
	mas.setBounds(new Rectangle(393, 77, 70, 40));
//	mas.setFont(new Font("Dialog", Font.BOLD, 14));
        mas.setLocation(corX, corY);
        PosicionBotonPanel1();
        //corY+=;
        mBotones.add(mas);
        mas.setText("Tarea " + idtarea);
        
        mas.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //mas.setComponentPopupMenu(jPopupMenu1);
                    JButton temp = (JButton) e.getSource();
                    IdTarea = Integer.parseInt(temp.getText().substring(6, 7));
                    //JOptionPane.showMessageDialog(null, "le diste clic al boton " + IdTarea);
                    Object[] opciones = {"Elimnar Tarea","Detalle de Tarea","Pasar al siguiente Sprint","Regresar al Sprint Anterior"};
                    
                    int resultado = JOptionPane.showOptionDialog(null, "Eliga lo que desea hacer","Tarea "+ IdTarea, JOptionPane.INFORMATION_MESSAGE, JOptionPane.QUESTION_MESSAGE,null, opciones, opciones[0]);
                    if (opciones[0] == opciones[resultado]) {
                      //---- eliminar Tarea 
                      mBotones.remove(IdTarea);
                      int Posicion=temp.getLocationOnScreen().x;
                      temp.setVisible(false);
                      if (Posicion >= 12 && Posicion <=222){
                            eliminarPanel(jPanel1,temp,"Inicio",0);
                      }
                      if (Posicion >= 345 && Posicion <= 555)
                            eliminarPanel(jPanel2,temp,"Proceso",1);
                      if (Posicion > 555 ) 
                            eliminarPanel(jPanel3,temp,"Finalizado",2);
                      
                    }
                    if (opciones[1]== opciones[resultado]){
                        //------Detalle de la Tarea otro form
                    }
                    
                    if (opciones[2]== opciones[resultado]){
                        //-----pasar al otro panel;
                        int Posicion=temp.getLocationOnScreen().x;
                        if (Posicion >= 12 && Posicion <=222){
                            PasarAlSegundoSprint(temp);
                            procs.ActualizaEstadoTarea(IdTarea, 2);
                        }
                        if (Posicion >= 345 && Posicion <= 555){ 
                            PasarAlTercerSprint(temp);
                            procs.ActualizaEstadoTarea(IdTarea, 3);
                        }
                    }
                    
                    if (opciones[3]== opciones[resultado]){
                        //----Regreas la tarea al Sprint Anterior
                        int Posicion=temp.getLocationOnScreen().x;
                        if (Posicion >= 12 && Posicion <=222)
                            JOptionPane.showMessageDialog(null, "Error no se puede Regreas mas la tarea", "Error", JOptionPane.ERROR_MESSAGE);
                        if (Posicion >= 345 && Posicion <= 555){
                            RegresarAlPrimerSprint(temp);
                            procs.ActualizaEstadoTarea(IdTarea,1);
                        }
                        if (Posicion > 555 ){ 
                            RegresarAlSegundoSprint(temp);
                            procs.ActualizaEstadoTarea(IdTarea, 2);
                        }
                    }
                }
        });
                 
    }
    
    
    
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tablero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tablero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tablero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tablero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Tablero(1).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAgregarTarea;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    // End of variables declaration//GEN-END:variables

    private void PasarAlSegundoSprint(JButton temp){
        temp.setVisible(false);
        jPanel2.add(temp);
        jPanel1.remove(temp);
        temp.setVisible(true);
        QuitarColorPanel();
     //   ColorPanel(jPanel1,"Inicio",0);
    }
    
    
    private void PasarAlTercerSprint(JButton temp){
        temp.setVisible(false);
        jPanel3.add(temp);
        jPanel2.remove(temp);
        temp.setVisible(true);
        QuitarColorPanel();
  //      ColorPanel(jPanel2,"Proceso",1);
    }
    
    private void RegresarAlSegundoSprint(JButton temp){
        temp.setVisible(false);
        jPanel2.add(temp);
        jPanel3.remove(temp);
        temp.setVisible(true);
         QuitarColorPanel();
   //     ColorPanel(jPanel3,"Finalizado",2);
    }
    
    private void RegresarAlPrimerSprint(JButton temp){
        temp.setVisible(false);
        jPanel1.add(temp);
        jPanel2.remove(temp);
        temp.setVisible(true);
        QuitarColorPanel();
      //  ColorPanel(jPanel2,"Proceso",1);
    }
    
    private void eliminarPanel(JPanel Panel, JButton temp,String a,int pos){
        jPanel1.remove(temp);
        QuitarColorPanel();
        ColorPanel(Panel,a,pos);
        jPanel1.setVisible(false);
        jPanel1.setVisible(true);
        QuitarColorPanel();
     //   ColorPanel(Panel,a,pos);
    }
    
    private void ColorPanel(JPanel Panel, String a, int pos){
        colorpan[pos] = new TextColorPanel();
        colorpan[pos].setSize(Panel.getSize());
        colorpan[pos].SetLetra(a);
        colorpan[pos].setVistaColor();
        Panel.add(colorpan[pos]);
    }
    
    private void QuitarColorPanel(){
     //   jPanel1.remove(colorpan[0]);
     //   jPanel2.remove(colorpan[1]);
     //   jPanel3.remove(colorpan[2]);
        jPanel1.setVisible(false);
        jPanel1.setVisible(true);
        jPanel2.setVisible(false);
        jPanel2.setVisible(true);
        jPanel3.setVisible(false);
        jPanel3.setVisible(true);
     //   ColorPanel(jPanel1,"Inicio",0);
     //   ColorPanel(jPanel2,"Proceso",1);
     //   ColorPanel(jPanel3,"Finalizado",2);
        
        
    }
}
